<form  class="layui-form padding-r20" id="add-rule-body">
    <div class="layui-form-item">
        <label class="layui-form-label">名称：</label>
        <div class="layui-input-block">
            <input type="text" model="rule_name" required placeholder="请输入规则名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">标识：</label>
        <div class="layui-input-block">
            <input type="text" model="rule_key" required placeholder="请输入规则标识" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">分组：</label>
        <div class="layui-input-block">
            <select model="rule_group_id" id="add-rule_group_select" lay-verify="" lay-search>

            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">简介</label>
        <div class="layui-input-block">
            <textarea model="rule_info"  placeholder="请输入简介" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" type="button" onclick="addRule()" id="btn-add-rule">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    layui.use('form',function(){
        var form = layui.form;
        $.post(api.rule_group.get,function(res){
            $("#add-rule_group_select").html('<option value="">请选择分组</option>');
            $.each(res.data,function(index,item){
                var rule_group_item = '<option value="' + item.rule_group_id + '">' + item.rule_group_name + '</option>';
                $("#add-rule_group_select").append(rule_group_item);
            });
            form.render();
        },'json');
    });
    function addRule(){
        var data = getModel({elem : "#add-rule-body"});
        var validate = new Validate();
        var check = validate.check(data,[
            ['rule_name','require','规则名称'],
            ['rule_key','require','规则标识'],
            ['rule_group_id','require','所属分组'],
            ['rule_info','require','规则简介']
        ]);
        if(!check){
            msg(validate.getError());
            return false;
        }
        $.post(api.rule.add,data,function(res){
            if(res.code){
                reloadTable('rules');
            }
            msg(res.msg);
        },'json');
    }
</script>